package cst.a000tyy.recyclerviewtest.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a000tyy.recyclerviewtest.R;
import cst.a000tyy.recyclerviewtest.bean.ArticleBean;
import cst.a000tyy.recyclerviewtest.adapter.MyAdapter;
import cst.a000tyy.recyclerviewtest.iface.ArticleListener;
import cst.a000tyy.recyclerviewtest.modal.ArticleModel;

public class MainActivity extends AppCompatActivity implements ArticleListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
        ArticleModel articlemodel=new ArticleModel();
        articlemodel.getResultLIist("article",2,"2ts2j910hv4m270hipde4a67d7",this);
    }
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter mAdapter;
    List<ArticleBean> list;
    private void initRecyclerView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);    //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(this);    //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);   //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);   //创建并设置Adapter
        mAdapter = new MyAdapter(this);
        mAdapter.setList(list);
        recyclerView.setAdapter(mAdapter);
    }


    @Override
    public void onResponse(List<ArticleBean> list) {
        this.list=list;
        mAdapter.setList(list);
        //  mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "fail:"+msg, Toast.LENGTH_SHORT).show();
    }
}
